package com.hice.demo.sbdbra.common.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class StrConverterUtil
{
  public static String str2HexStr(String str)
  {
    char[] chars = "0123456789ABCDEF".toCharArray();
    StringBuilder sb = new StringBuilder("");
    byte[] bs = str.getBytes();

    for (int i = 0; i < bs.length; i++) {
      int bit = (bs[i] & 0xF0) >> 4;
      sb.append(chars[bit]);
      bit = bs[i] & 0xF;
      sb.append(chars[bit]);
      sb.append(' ');
    }
    return sb.toString().trim();
  }

  public static String hexStr2Str(String hexStr)
  {
    String str = "0123456789ABCDEF";
    char[] hexs = hexStr.toCharArray();
    byte[] bytes = new byte[hexStr.length() / 2];

    for (int i = 0; i < bytes.length; i++) {
      int n = str.indexOf(hexs[(2 * i)]) * 16;
      n += str.indexOf(hexs[(2 * i + 1)]);
      bytes[i] = (byte)(n & 0xFF);
    }
    return new String(bytes);
  }

  public static String byte2HexStr(byte[] b)
  {
    String stmp = "";
    StringBuilder sb = new StringBuilder("");
    for (int n = 0; n < b.length; n++) {
      stmp = Integer.toHexString(b[n] & 0xFF);
      sb.append(stmp.length() == 1 ? new StringBuilder().append("0").append(stmp).toString() : stmp);
      sb.append(" ");
    }
    return sb.toString().toUpperCase().trim();
  }

  public static byte[] hexStr2Bytes(String src)
  {
    int m = 0; int n = 0;
    int l = src.length() / 2;
    System.out.println(l);
    byte[] ret = new byte[l];
    for (int i = 0; i < l; i++) {
      m = i * 2 + 1;
      n = m + 1;
      ret[i] = Byte.decode(new StringBuilder().append("0x").append(src.substring(i * 2, m)).append(src.substring(m, n)).toString()).byteValue();
    }
    return ret;
  }

  public static String strToUnicode(String strText)
    throws Exception
  {
    StringBuilder str = new StringBuilder();

    for (int i = 0; i < strText.length(); i++) {
      char c = strText.charAt(i);
      int intAsc = c;
      String strHex = Integer.toHexString(intAsc);
      if (intAsc > 128) {
        str.append(new StringBuilder().append("\\u").append(strHex).toString());
      }
      else
        str.append(new StringBuilder().append("\\u00").append(strHex).toString());
    }
    return str.toString();
  }

  public static String unicodeToString(String hex)
  {
    int t = hex.length() / 6;
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < t; i++) {
      String s = hex.substring(i * 6, (i + 1) * 6);

      String s1 = new StringBuilder().append(s.substring(2, 4)).append("00").toString();

      String s2 = s.substring(4);

      int n = Integer.valueOf(s1, 16).intValue() + Integer.valueOf(s2, 16).intValue();

      char[] chars = Character.toChars(n);
      str.append(new String(chars));
    }
    return str.toString();
  }

  public static Long strHash(String key)
  {
    ByteBuffer buf = ByteBuffer.wrap(key.getBytes());
    int seed = 305441741;

    ByteOrder byteOrder = buf.order();
    buf.order(ByteOrder.LITTLE_ENDIAN);

    long m = -4132994306676758123L;
    int r = 47;

    long h = seed ^ buf.remaining() * m;

    while (buf.remaining() >= 8) {
      long k = buf.getLong();

      k *= m;
      k ^= k >>> r;
      k *= m;

      h ^= k;
      h *= m;
    }

    if (buf.remaining() > 0) {
      ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);

      finish.put(buf).rewind();
      h ^= finish.getLong();
      h *= m;
    }

    h ^= h >>> r;
    h *= m;
    h ^= h >>> r;

    buf.order(byteOrder);
    return Long.valueOf(h);
  }
}